define(['../module','../../services/anon/authService','../../services/anon/login_service'],
    function (controllers) {
    "use strict";

    controllers.controller('loginCtrl',['$rootScope','$scope', '$state','toaster','$http', '$timeout',"IsBackEndOk",'loginFactory', function ($rootScope,$scope, $state,toaster,$http, $timeout,IsBackEndOk,loginFactory) {
        console.log('loginCtrl');
        console.log('IsBackEndOk:'+IsBackEndOk);
        $rootScope.isLogged = false;

        $scope.login = {
            working: false,
            wrong: false
        };

        $scope.loginFn = function (user) {


            if(IsBackEndOk == 'true')
            {
                console.log('OK:'+IsBackEndOk);
                var loginUser = {
                    "user":user
                };
                loginFactory.doLogin(loginUser)
                    .success(function (success) {
                        console.log("success:"+success);
                        if(success.resultCode == 200){

                            $scope.status = '登录成功';
                            console.log("doLogin");
                            $state.go('site.index');

                        }else if (success.resultCode == 102) {
                            toaster.pop('error', "操作失败", success.resultReason);
                            $scope.status = success.resultReason;
                        };

                    })
                    .error(function (error) {
                        $scope.status = '登录失败: ' + error.message;
                        console.log($scope.status);
                    });
            }else{
                $scope.login.working = true;
                $scope.login.wrong = false;

                $scope.auth.login(
                    $scope.login,
                    function(res) {
                        $rootScope.isLogged = true;
                        $state.go('site.index');
                        $rootScope.error = null;
                    },
                    function(err){
                        $scope.login.wrong = true;
                        $timeout(function () {
                                $scope.login.wrong = false;
                            },
                            2000);
                    },
                    function(){
                        $scope.login.working = false;
                    }
                );
            }

        };

        $scope.logout = function () {

            if(IsBackEndOk == 'true')
            {
                console.log('OK:'+IsBackEndOk);
                var logoutUser = {
                    "user":user
                };
                loginFactory.logout(logoutUser)
                    .success(function (success) {
                        console.log("success:"+success);
                        if(success.resultCode == 200){

                            $scope.status = '成功退出';
                            console.log("logout success");
                            $state.go('site.index');

                        }else if (success.resultCode == 102) {
                            toaster.pop('error', "退出失败", success.resultReason);
                            $scope.status = success.resultReason;
                        };

                    })
                    .error(function (error) {
                        $scope.status = '退出失败: ' + error.message;
                        console.log($scope.status);
                    });
            }else{
                $scope.auth.logoutUser(
                    function(){
                        $rootScope.isLogged = false;
                        $state.go('site.index');
                        $rootScope.error = null;
                    },
                    function(){
                        $rootScope.error('退出失败！');
                    }
                );
            }

        };


    }])
});